package com.heepay;

import com.heepay.common.HttpUtil;
import com.heepay.common.MyKey;
import com.heepay.common.SignEncrypt;
import org.bouncycastle.util.encoders.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 该接口为确认支付接口
 *
 */

public class ConfirmPay {
    public static void main(String[] args) throws Exception {

        //商户编号，（汇付宝商户内码：七位整数数字）
        String agent_id = "1664502";
        //二级商户号，传了要参与签名（集团商户模式）
        String ref_agent_id = "";
        //版本号，固定为1
        String version = "1";
        //支付订单创建成功的标记（短信接口返回）
        String hy_token_id = "";
        //支付短信验证码
        String verify_code = "";

        //拼接待加密签名参数

        String signStr = "hy_token_id=" + hy_token_id +
                "&ref_agent_id=" + ref_agent_id +
                "&verify_code=" + verify_code +
                "&version=" + version;

        System.out.println("待加密签名串：--------->" + signStr);

        SignEncrypt signEncrypt = new SignEncrypt();
        // 使用类名和常量名来访问常量
        String publicKeystr = MyKey.publicKeystr;
        String privateKeystr = MyKey.privateKeystr;
        //密钥配置
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] publicKeyPath = Base64.decode(publicKeystr.getBytes());
        byte[] privateKeyPath = Base64.decode(privateKeystr.getBytes());
        PublicKey heepayPublicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyPath));
        PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyPath));
        //加密
        String content = signEncrypt.encrypt(signStr, heepayPublicKey);
        //加签
        String sign = signEncrypt.sign(signStr, privateKey);

        Map<String, String> map = new HashMap<>();
        map.put("encrypt_data", content);
        map.put("agent_id",agent_id);
        map.put("sign",sign);

        System.out.println("请求参数：--------->" + map);

        HttpUtil httpUtil = new HttpUtil();
        String Result = httpUtil.sendPost("Https://Pay.Heepay.com/WithholdAuthPay/ConfirmPay.aspx",map);
        System.out.println("响应参数：--------->" + Result);
    }

}
